package com.bby.user.controller;

import cn.hutool.core.util.ObjectUtil;
import com.bby.common.exception.BBYException;
import com.bby.common.model.FavoriteGroup;
import com.bby.common.model.PmMessage;
import com.bby.common.model.UserSettings;
import com.bby.common.utils.R;
import com.bby.common.utils.StatusCode;
import com.bby.common.aop.ParseToken;
import com.bby.user.bean.req.FavoriteActionReq;
import com.bby.user.bean.req.UserGroupActionReq;
import com.bby.user.service.UserActionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户-用户动作
 */
@Controller
@RequestMapping("action")
public class UserActionController {

    private final UserActionService userActionService;
    Logger logger = LoggerFactory.getLogger(this.getClass());
    public UserActionController(UserActionService userActionService) {
        this.userActionService = userActionService;
    }


    /**
     * 收藏操作
     * @param params 参数
     */
    @ResponseBody
    @ParseToken
    @PostMapping("favorites")
    public R favorite(String userId,@RequestBody FavoriteActionReq params) throws BBYException {
        System.out.println(params);
        if (ObjectUtil.isEmpty(params.getOpera())){
            return R.error(StatusCode.INPUT_ERROR,"输入参数错误");
        }
        return R.ok(userActionService.favoriteOp(userId,params));
    }
    /**
     * 获取收藏书签
     */
    @ParseToken
    @ResponseBody
    @GetMapping("favorites")
    public R getFavorites(String userId) throws BBYException {
        if (ObjectUtil.isEmpty(userId)){
            return R.error(StatusCode.NOT_LOGIN,"未登录");
        }
        List<FavoriteGroup> res = userActionService.getFavorites(userId);
        return R.ok(res);
    }
    /**
     * 用户组操作
     * @param params 参数
     */
    @ParseToken
    @ResponseBody
    @PostMapping("userGroups")
    public R userGroups(String userId,@RequestBody UserGroupActionReq params) throws BBYException {
        System.out.println(params);
        if (ObjectUtil.isEmpty(params.getOpera())){
            return R.error(StatusCode.INPUT_ERROR,"输入参数错误");
        }
        return R.ok(userActionService.userGroups(userId,params));
    }
    /**
     * 获取用户分组
     */
    @ParseToken
    @ResponseBody
    @GetMapping("userGroups")
    public R getUserGroups(String userId) throws BBYException {
        if (ObjectUtil.isEmpty(userId)){
            return R.error(StatusCode.NOT_LOGIN,"未登录");
        }
        return R.ok(userActionService.getUserGroups(userId));
    }
    /**
     * 设置用户设置
     * @param settings
     * @return
     */
    @ParseToken
    @ResponseBody
    @PostMapping("settings")
    public R settings(String userId, @RequestBody UserSettings settings) throws BBYException {
        if (ObjectUtil.isEmpty(userId)){
            return R.error(StatusCode.NOT_LOGIN,"未登录");
        }
        return R.ok(userActionService.setUserSettings(userId,settings));
    }
    /**
     * 获取用户设置
     * @return json数据
     */
    @ParseToken
    @ResponseBody
    @GetMapping("settings")
    public R getSettings(String userId) throws BBYException {
        if (ObjectUtil.isEmpty(userId)) {
            return R.error(StatusCode.NOT_LOGIN,"未登录");
        }
        return R.ok(userActionService.getUserSettings(userId));
    }

    /**
     * 搜索用户
     * @param regex 正则
     * @param page 分页，默认 1，最小 1
     * @param limit 分页，默认 12，最小 5
     * @return
     * @throws BBYException
     */
    @ResponseBody
    @GetMapping("search")
    public R search(String regex,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "12") Integer limit)  {
        if (ObjectUtil.isEmpty(regex)){
            return R.ok(new ArrayList<>());
        }
        if (limit<5){
            limit=5;
        }
        return R.ok(userActionService.searchUser(regex,page,limit));
    }

}
